<?php
/**
 * 文件详情展示页面
 */

require_once 'config/config.php';
require_once 'includes/db.php';

// 检查是否已安装
if (!file_exists(__DIR__ . '/config/installed.lock')) {
    header('Location: install.php');
    exit;
}

$fileId = $_GET['id'] ?? null;
if (!$fileId) {
    header('Location: index.php');
    exit;
}

try {
    $db = new Database();
    $file = $db->getFileDetails($fileId);

    if (!$file) {
        header('Location: index.php');
        exit;
    }
} catch (Exception $e) {
    die('加载文件信息失败: ' . $e->getMessage());
}

/**
 * 格式化文件大小
 */
function formatFileSize($bytes) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= (1 << (10 * $pow));
    return round($bytes, 2) . ' ' . $units[$pow];
}

/**
 * 获取文件图标
 */
function getFileIcon($ext) {
    $icons = [
        'pdf' => '📄', 'doc' => '📝', 'docx' => '📝', 'txt' => '📃',
        'jpg' => '🖼️', 'jpeg' => '🖼️', 'png' => '🖼️', 'gif' => '🖼️',
        'zip' => '🗜️', 'rar' => '🗜️', '7z' => '🗜️',
        'mp4' => '🎬', 'avi' => '🎬', 'mov' => '🎬',
        'mp3' => '🎵', 'wav' => '🎵', 'flac' => '🎵',
        'xls' => '📊', 'xlsx' => '📊', 'ppt' => '📽️', 'pptx' => '📽️'
    ];
    return $icons[strtolower($ext)] ?? '📦';
}

// 获取显示用的信息
$displayTitle = $file['page_title'] ?: $file['file_title'] ?: $file['original_name'];
$displayDescription = $file['page_description'] ?: $file['file_description'];
$fileName = $file['file_title'] ?: $file['original_name'];
$fileDescription = $file['file_description'];

// 处理下载
if (isset($_POST['download']) || isset($_GET['download'])) {
    header('Location: download.php?id=' . $file['id']);
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($displayTitle) ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f5f7;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
        }

        .download-page {
            background: white;
            border-radius: 20px;
            padding: 60px 40px;
            box-shadow: 0 8px 40px rgba(0,0,0,0.08);
            margin-top: 40px;
        }

        .page-header {
            text-align: center;
            margin-bottom: 50px;
        }

        .page-icon {
            font-size: 80px;
            margin-bottom: 24px;
        }

        .page-title {
            font-size: 42px;
            font-weight: 700;
            color: #1d1d1f;
            margin-bottom: 16px;
        }

        .page-description {
            font-size: 18px;
            color: #86868b;
            line-height: 1.6;
            max-width: 600px;
            margin: 0 auto;
        }

        .file-info {
            background: #f5f5f7;
            border-radius: 16px;
            padding: 32px;
            margin-bottom: 40px;
        }

        .file-header {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 24px;
        }

        .file-icon {
            font-size: 64px;
        }

        .file-details h2 {
            font-size: 28px;
            font-weight: 600;
            color: #1d1d1f;
            margin-bottom: 8px;
        }

        .file-meta {
            display: flex;
            gap: 24px;
            color: #86868b;
            font-size: 14px;
        }

        .file-meta-item {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .file-description {
            color: #515154;
            font-size: 16px;
            line-height: 1.8;
            padding-top: 24px;
            border-top: 1px solid #d2d2d7;
        }

        .download-section {
            margin-top: 40px;
        }

        .download-button {
            width: 100%;
            padding: 20px 40px;
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            box-shadow: 0 4px 20px rgba(0, 122, 255, 0.3);
            text-decoration: none;
        }

        .download-button:hover {
            background: #0056CC;
            transform: translateY(-2px);
            box-shadow: 0 6px 30px rgba(0, 122, 255, 0.4);
        }

        .download-info {
            text-align: center;
            margin-top: 16px;
            color: #86868b;
            font-size: 14px;
        }

        .stats-bar {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-top: 30px;
            padding-top: 30px;
            border-top: 1px solid #d2d2d7;
        }

        .stat-item {
            text-align: center;
        }

        .stat-value {
            font-size: 24px;
            font-weight: 700;
            color: #1d1d1f;
            margin-bottom: 4px;
        }

        .stat-label {
            font-size: 14px;
            color: #86868b;
        }

        .back-link {
            position: fixed;
            bottom: 20px;
            right: 20px;
            padding: 12px 24px;
            background: rgba(0, 122, 255, 0.1);
            color: #007AFF;
            border-radius: 20px;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
        }

        .back-link:hover {
            background: rgba(0, 122, 255, 0.2);
        }

        @media (max-width: 768px) {
            .download-page {
                padding: 40px 24px;
                margin-top: 20px;
            }

            .page-title {
                font-size: 32px;
            }

            .file-header {
                flex-direction: column;
                text-align: center;
            }

            .file-details h2 {
                font-size: 22px;
            }

            .stats-bar {
                flex-direction: column;
                gap: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="download-page">
            <div class="page-header">
                <div class="page-icon">☁️</div>
                <h1 class="page-title"><?= htmlspecialchars($displayTitle) ?></h1>
                <?php if ($displayDescription): ?>
                    <p class="page-description"><?= nl2br(htmlspecialchars($displayDescription)) ?></p>
                <?php endif; ?>
            </div>

            <div class="file-info">
                <div class="file-header">
                    <div class="file-icon"><?= getFileIcon($file['file_ext'] ?? '') ?></div>
                    <div class="file-details">
                        <h2><?= htmlspecialchars($fileName) ?></h2>
                        <div class="file-meta">
                            <div class="file-meta-item">
                                <span>📏</span>
                                <span><?= formatFileSize($file['file_size']) ?></span>
                            </div>
                            <div class="file-meta-item">
                                <span>📅</span>
                                <span><?= date('Y-m-d', strtotime($file['upload_time'])) ?></span>
                            </div>
                        </div>
                    </div>
                </div>
                <?php if ($fileDescription): ?>
                    <div class="file-description">
                        <?= nl2br(htmlspecialchars($fileDescription)) ?>
                    </div>
                <?php endif; ?>
            </div>

            <div class="download-section">
                <form method="POST">
                    <button type="submit" name="download" class="download-button">
                        <span style="font-size: 24px;">⬇️</span>
                        <span>立即下载</span>
                    </button>
                </form>
                <div class="download-info">
                    点击下载按钮即可获取文件
                </div>
            </div>

            <div class="stats-bar">
                <div class="stat-item">
                    <div class="stat-value"><?= $file['download_count'] ?></div>
                    <div class="stat-label">下载次数</div>
                </div>
                <div class="stat-item">
                    <div class="stat-value">100%</div>
                    <div class="stat-label">安全可靠</div>
                </div>
            </div>
        </div>
    </div>

    <a href="index.php" class="back-link">返回首页</a>
</body>
</html>
